import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Http, Response, Headers, RequestOptions, URLSearchParams} from '@angular/http';
import {Observable, Subject} from 'rxjs';
import {map} from 'rxjs/operators';

@Injectable()
export class ConfigService {

    /**用于在路由的环境下与父组件通讯 */
    private routeMsgPushSource = new Subject<string>();
    routeMsgPush$ = this.routeMsgPushSource.asObservable();

    constructor(private http: HttpClient) {
    }

    /** 获取指定业务下的全部箱子 */
    getTaskListTree(bizId: number, taskListGroupId: number, system: string) {
        return this.http.get('/' + system + '/api/tasklist/listtree?bizId=' + bizId + '&taskListGroupId='+ taskListGroupId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**获取栏目配置信息 */
    showTaskListDetail(taskListId: number, system: string) {
        return this.http.get('/' + system + '/api/tasklist/detail?taskListId=' + taskListId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**保存或更新栏目配置 */
    saveUpdateTaskList(taskList: any, system: string) {
        return this.http.post('/' + system + '/api/tasklist/saveupdate', taskList).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**删除栏目配置 */
    deleteTaskList(taskListId: number, system: string) {
        return this.http.get('/' + system + '/api/tasklist/delete?taskListId=' + taskListId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**获取栏目字段 */
    showTaskListFields(taskListId: number, system: string) {
        return this.http.get('/' + system + '/api/tasklist/fields/list?taskListId=' + taskListId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**保存栏目字段 */
    updateTaskListFields(fields: any[], system: string) {
        return this.http.post('/' + system + '/api/tasklist/fields/update', fields).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**获取流程全部参与者 */
    listParts(procDefId: number, taskListId: number, system: string) {
        return this.http.get('/' + system + '/api/tasklist/part/list?procDefId=' + procDefId + '&taskListId=' + taskListId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**保存流程参与者与栏目关系 */
    saveUpdateTaskListPart(taskListId: number, procDefId: number, partIds: string, system: string) {
        return this.http.get('/' + system + '/api/tasklist/part/saveupdate?taskListId=' + taskListId + '&procDefId=' + procDefId + '&partIds=' + partIds).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**根据业务获取流程 */
    listProcByBizId(bizId: number, procDefId: number, systemUrl: string) {
        return this.http.get('/' + systemUrl + '/api/tasklist/part/listprocs?bizId=' + bizId + '&procDefId='+procDefId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 获取指定业务下的流程、环节、参与者 */
    getProcActPartTreeByBizId(bizId: number, procDefId: number, systemUrl: string) {
        return this.http.get('/' + systemUrl + '/api/tasklist/menu/listprocactparts?bizId=' + bizId + '&procDefId='+procDefId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 获取指定业务下的全部信息 */
    loadMenuGroups(systemUrl: string) {
        return this.http.get('/' + systemUrl + '/api/tasklist/menu/groups').pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 获取指定环节的菜单权限 */
    loadMenuAuthority(taskListId: number, actDefId: number, systemUrl: string) {
        return this.http.get('/' + systemUrl + '/api/tasklist/menu/listauthority?taskListId=' + taskListId + '&actDefId=' + actDefId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 保存指定环节下的菜单权限信息 */
    saveUpdateMenuAuthority(params: any, actDefId: number, taskListId: number, systemUrl: string) {
        return this.http.post('/' + systemUrl + '/api/tasklist/menu/saveupdateauth?actDefId=' + actDefId + '&taskListId=' + taskListId, params).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 获取指定业务下的全部信息 */
    loadTabMenus(systemUrl: string) {
        return this.http.get('/' + systemUrl + '/api/tasklist/tab/tabmenus').pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 获取指定环节的菜单权限 */
    loadTabAuthority(taskListId: number, actDefId: number, systemUrl: string) {
        return this.http.get('/' + systemUrl + '/api/tasklist/tab/listauthority?taskListId=' + taskListId + '&actDefId=' + actDefId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 保存指定环节下的菜单权限信息 */
    saveUpdateTabAuthority(params: any, actDefId: number, taskListId: number, systemUrl: string) {
        return this.http.post('/' + systemUrl + '/api/tasklist/tab/saveupdateauth?actDefId=' + actDefId + '&taskListId=' + taskListId, params).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 获取分组信息 */
    listTaskListGroup(systemUrl: string) {
        return this.http.get('/' + systemUrl + '/api/tasklist/group').pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 新增栏目 */
    addTaskList(tasklist: any, systemUrl: string) {
        return this.http.post('/' + systemUrl + '/api/tasklist/add', tasklist).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 获取栏目列表 */
    getTabList(taskListId: number, actDefId: number, partId: number, systemUrl: string) {
        return this.http.get('/' + systemUrl + '/api/tasklist/tab/getTabList?taskListId=' + taskListId + '&actDefId=' + actDefId + '&partId=' + partId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 获取栏目栏目列表 */
    getTabMenuList(taskListId: number, actDefId: number, partId: number, tabId: number, systemUrl: string) {
        return this.http.get('/' + systemUrl + '/api/tasklist/tab/getTabMenuList?tabId=' + tabId + '&taskListId=' + taskListId + '&actDefId=' + actDefId + '&partId=' + partId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 保存标签权限 */
    saveTabPart(param: any, systemUrl: string) {
        return this.http.post('/' + systemUrl + '/api/tasklist/tab/saveTabPart', param).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 保存标签菜单权限 */
    saveTabMenuPart(param: any, systemUrl: string) {
        return this.http.post('/' + systemUrl + '/api/tasklist/tab/saveTabMenuPart', param).pipe(
            map((res: Response) => res['data'])
        );
    }


    /**父子组件通讯 */
    announceMission(data: string) {
        this.routeMsgPushSource.next(data);
    }
}
